package me.sealer.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class IdUtil {

    /**
     * 身份证18位转15位
     *
     * @param idNo
     * @return String
     * @author sealer
     */
    public static String convertIdNoToShort(String idNo) {
        if (idNo.length() != 18) {
            return "";
        }
        // 去除年份前2位，和最后一位
        return idNo.substring(0, 6) + idNo.substring(8, 17);
    }

    /**
     * 身份证15位转18位
     *
     * @param idNo
     * @return String
     * @author sealer
     */
    public static String convertIdNoToLong(String idNo) {
        if (idNo.length() != 15) {
            return "";
        }
        idNo = idNo.substring(0, 6) + "19" + idNo.substring(6);
        // 加权因子
        int[] wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
        // 校验码
        String[] vi = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
        int sum = 0;
        for (int i = 0; i < idNo.length(); i++) {
            sum = sum + wi[i] * Integer.valueOf(idNo.substring(i, i + 1)).intValue();
        }
        int y = sum % 11;
        return idNo + vi[y];
    }

    /**
     * {身份证15位转18位}
     *
     * @param IdNo
     * @return
     * @version 1.0
     * @note 江苏农村信用社联合社提供的转换方法
     * @since 1.0
     */
    public static String convert15to18(String IdNo) {
        String sID15 = IdNo;
        String sID18 = "";
        String sTemp = "";
        if (sID15 == null || sID15.trim().length() != 15) {
            sID18 = "";
        } else {
            sID15 = sID15.trim();
            try {
                sTemp = sID15.substring(0, 6) + "19" + sID15.substring(6);
                sID18 = sTemp + getVerify(sTemp);
            } catch (Exception e) {
                sID18 = "";
            }
        }
        return sID18;
    }

    /**
     * {返回18位最后一位验证码}
     *
     * @param s17
     * @return
     * @throws Exception
     * @version 1.0
     * @note 江苏农村信用社联合社提供的转换方法
     * @since 1.0
     */
    private static String getVerify(String s17) throws Exception {
        String sVerify = "";
        String code = "";
        int num = 0;
        int tmp = 0;
        for (int i = 18; i >= 2; i--) {
            num += (Math.pow(2, i - 1) % 11) * Integer.parseInt(s17.substring(18 - i, 18 - i + 1));
        }
        num %= 11;
        switch (num) {
            case 0:
                code = "1";
                break;
            case 1:
                code = "0";
                break;
            case 2:
                code = "X";
                break;
            default:
                tmp = 12 - num;
                code = Integer.toString(tmp);
                break;
        }
        sVerify = code;
        return sVerify;
    }

    /**
     * {取得身份证号上的出生日期}
     *
     * @param idNo 15位或18位身份证号
     * @return java.sql.Date 出生日期
     * @version 1.0
     * @since 1.0
     */
    public static Date getBirthday(String idNo) {
        String birthDay = "";
        Date date = null;
        if (idNo.length() == 15) {
            birthDay = "19" + idNo.substring(6, 8) + '-' + idNo.substring(8, 10) + '-' + idNo.substring(10, 12);
        } else {
            birthDay = idNo.substring(6, 10) + '-' + idNo.substring(10, 12) + '-' + idNo.substring(12, 14);
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            date = sdf.parse(birthDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

    /**
     * 根据身份证件取性别
     *
     * @param idno
     * @return
     */
    public static String getGenderByIdNo(String idno) {
        if (idno == null || idno.isEmpty())
            return "";
        if (idno.length() != 15 && idno.length() != 18)
            return "";

        // 身份证号码为15位
        if (idno.length() == 15) {
            int i = Integer.valueOf(idno.substring(14));
            // 最后一位数字进行判断，偶数为女性，奇数为男性
            if (i % 2 == 0) {
                return "F";
            } else {
                return "M";
            }
        }
        // 身份证号码为18位
        if (idno.length() == 18) {
            int i = Integer.valueOf(idno.substring(16, 17));
            // 倒数第2位数字进行判断，偶数为女性，奇数为男性
            if (i % 2 == 0) {
                return "F";
            } else {
                return "M";
            }
        }
        return "";
    }
}
